#include "ScreenManager.h"
#include "IHudElement.h"

ScreenManager::ScreenManager(EngineStuff* eng)
{
	engine = eng;
	currentScreen = NULL;
}

ScreenManager::~ScreenManager(void)
{

}

void ScreenManager::ActiveScreen(IScreen* screen)
{
	vector<IScreen*>::iterator it =  std::find(screens.begin(),screens.end(),screen);
	if(it != screens.end())
	{		
		if(currentScreen != NULL)
			currentScreen->Release();
		currentScreen = screen;
		currentScreen->Init(engine);
	}	
}

void ScreenManager::Update(DWORD elapsedTime)
{
	for(int i =0; i < screens.size();i++)
	{
		if(screens[i]->updateWhenNotCurrentScreen == true || screens[i] == currentScreen)
		{
			screens[i]->Update(elapsedTime);
		}
	}
}

void ScreenManager::Draw(DWORD elapsedTime)
{
	if(currentScreen != NULL)
		currentScreen->Draw(elapsedTime);
}

void ScreenManager::RemoveScreen(IScreen* screen)
{
	vector<IScreen*>::iterator it =  std::find(screens.begin(),screens.end(),screen);
	if(it != screens.end())
	{
		screens.erase(it);
	}
	
}

void ScreenManager::AddScreen(IScreen* screen)
{
	screens.push_back(screen);
}

IScreen::~IScreen()
{
	updateWhenNotCurrentScreen = false;
}

IScreen::IScreen()
{
}

void IScreen::Draw(DWORD ElapsedTime)
{
	for(list<IHudElement*>::iterator it  = elements.begin() ; it != elements.end() ; it++)
	{			
		it._Ptr->_Myval->Draw(ElapsedTime);
	}
}

void IScreen::Update(DWORD ElapsedTime)
{

	for(list<Updateable*>::iterator it  = updateables.begin() ; it != updateables.end() ; it++)
	{
		it._Ptr->_Myval->Update(ElapsedTime);
	}

	for(list<IHudElement*>::iterator it  = elements.begin() ; it != elements.end() ; it++)
	{			
		it._Ptr->_Myval->Update(ElapsedTime);
	}
}

